﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using System.Data.Common;

namespace Cashier_DAO
{
    public class ConfigDAO: DAOFactory
    {
        /// <summary>
        /// 清空数据库表数据
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="tables"></param>
        /// <returns></returns>
        public int EmptyTableData(DbConnection conn, String[] tables)
        {
            int emptyTableCount = 0;
            DbTransaction trans = null;
            try
            {
                if (tables != null && tables.Length > 0)
                {
                    trans = conn.BeginTransaction();
                    String sql = String.Empty;
                    DbCommand cmd = CommandFactory.CreateCommand();
                    cmd.Connection = conn;
                    foreach (String tb in tables)
                    {
                        sql = "delete from " + tb;
                        cmd.CommandText = sql;
                        cmd.ExecuteNonQuery();
                        emptyTableCount += 1;
                    }
                    trans.Commit();
                }
            }
            catch (Exception ex)
            {
                if (trans != null)
                    trans.Rollback();
                throw ex;
            }
            finally
            {
                trans = null;
            }
            return emptyTableCount;
        }
    }
}
